package com.bdqn.business.academic.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.business.academic.dao.AskMapper;
import com.bdqn.business.academic.dao.SelectionMapper;
import com.bdqn.business.academic.dao.SingleMapper;
import com.bdqn.business.academic.dao.UpperMapper;
import com.bdqn.entity.academic.Ask;
import com.bdqn.entity.academic.Selection;
import com.bdqn.entity.academic.Single;
import com.bdqn.response.Result;
import com.bdqn.response.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class QuestionService {

    @Autowired
    private SingleMapper singleMapper;

    @Autowired
    private SelectionMapper selectionMapper;

    @Autowired
    private AskMapper askMapper;


    public Result getQuestionsByChapterIdsAndQuestionTypeIds(Map<String, Object> map) {
        String chapterIds = map.get("chapterIds").toString(); //章节数组
        String questionTypeIds = map.get("questionTypeIds").toString(); //题型 1 2 3 4


        Integer singleCount = 0; //初始化单选题的数量
        Integer selectionCount = 0; //初始化多选题的数量
        Integer askCount = 0; //初始化问答题数量

        for (String chapterId : chapterIds.split(",")) {
            if(questionTypeIds.contains("1")){ //单选题
                QueryWrapper<Single> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("chapter_id",chapterId);
                queryWrapper.eq("type","1");
                singleCount += singleMapper.selectCount(queryWrapper);

            }
            if(questionTypeIds.contains("2")){ //多选题
                QueryWrapper<Selection> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("chapter_id",chapterId);
                queryWrapper.eq("type","2");
                selectionCount += selectionMapper.selectCount(queryWrapper);
            }
            if(questionTypeIds.contains("3")){ //问答题
                QueryWrapper<Ask> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("chapter_id",chapterId);
                queryWrapper.eq("type","3");
                askCount += askMapper.selectCount(queryWrapper);
            }
        }


        Map<String,Object> returnMap = new HashMap<>();
        returnMap.put("singleCount",singleCount);
        returnMap.put("selectionCount",selectionCount);
        returnMap.put("askCount",askCount);

        return new Result(ResultCode.SUCCESS,returnMap);
    }
}
